1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "InputWindowHandle"
18
19 #include "android_hardware_input_InputWindowHandle.h"
20
21 #include <android/graphics/matrix.h>
22 #include <android/graphics/region.h>
23 #include <android_runtime/AndroidRuntime.h>
24 #include <android_runtime/Log.h>
25 #include <binder/IPCThreadState.h>
26 #include <ftl/flags.h>
27 #include <gui/SurfaceControl.h>
28 #include <gui/WindowInfo.h>
29 #include <nativehelper/JNIHelp.h>
30 #include <ui/Region.h>
31 #include <utils/threads.h>
32
33 #include "SkRegion.h"
34 #include "android_hardware_input_InputApplicationHandle.h"
35 #include "android_util_Binder.h"
36 #include "core_jni_helpers.h"
37 #include "jni.h"
38
39 namespace android {
40
41 using gui::TouchOcclusionMode;
42 using gui::WindowInfo;
43
44 struct WeakRefHandleField {
45 jfieldID ctrl;
46 jmethodID get;
47 jfieldID mNativeObject;
48 };
49
50 static struct {
51 jclass clazz;
52 jmethodID ctor;
53 jfieldID ptr;
54 jfieldID inputApplicationHandle;
55 jfieldID token;
56 jfieldID name;
57 jfieldID layoutParamsFlags;
58 jfieldID layoutParamsType;
59 jfieldID dispatchingTimeoutMillis;
60 jfieldID frameLeft;
61 jfieldID frameTop;
62 jfieldID frameRight;
63 jfieldID frameBottom;
64 jfieldID surfaceInset;
65 jfieldID scaleFactor;
66 jfieldID touchableRegion;
67 jfieldID touchOcclusionMode;
68 jfieldID ownerPid;
69 jfieldID ownerUid;
70 jfieldID packageName;
71 jfieldID inputConfig;
72 jfieldID displayId;
73 jfieldID replaceTouchableRegionWithCrop;
74 WeakRefHandleField touchableRegionSurfaceControl;
75 jfieldID transform;
76 jfieldID windowToken;
77 jfieldID focusTransferTarget;
78 } gInputWindowHandleClassInfo;
79
80 static struct {
81 jclass clazz;
82 jmethodID ctor;
83 } gRegionClassInfo;
84
85 static Mutex gHandleMutex;
86
87
88 // --- NativeInputWindowHandle ---
89
NativeInputWindowHandle(jweak objWeak)90 NativeInputWindowHandle::NativeInputWindowHandle(jweak objWeak) :
91 mObjWeak(objWeak) {
92 }
93
~NativeInputWindowHandle()94 NativeInputWindowHandle::~NativeInputWindowHandle() {
95 JNIEnv* env = AndroidRuntime::getJNIEnv();
96 env->DeleteWeakGlobalRef(mObjWeak);
97
98 // Clear the weak reference to the layer handle and flush any binder ref count operations so we
99 // do not hold on to any binder references.
100 // TODO(b/139697085) remove this after it can be flushed automatically
101 mInfo.touchableRegionCropHandle.clear();
102 IPCThreadState::self()->flushCommands();
103 }
104
getInputWindowHandleObjLocalRef(JNIEnv * env)105 jobject NativeInputWindowHandle::getInputWindowHandleObjLocalRef(JNIEnv* env) {
106 return env->NewLocalRef(mObjWeak);
107 }
108
updateInfo()109 bool NativeInputWindowHandle::updateInfo() {
110 JNIEnv* env = AndroidRuntime::getJNIEnv();
111 jobject obj = env->NewLocalRef(mObjWeak);
112 if (!obj) {
113 releaseChannel();
114 return false;
115 }
116
117 mInfo.touchableRegion.clear();
118
119 jobject tokenObj = env->GetObjectField(obj, gInputWindowHandleClassInfo.token);
120 if (tokenObj) {
121 mInfo.token = ibinderForJavaObject(env, tokenObj);
122 env->DeleteLocalRef(tokenObj);
123 } else {
124 mInfo.token.clear();
125 }
126
127 mInfo.name = getStringField(env, obj, gInputWindowHandleClassInfo.name, "<null>");
128
129 mInfo.dispatchingTimeout = std::chrono::milliseconds(
130 env->GetLongField(obj, gInputWindowHandleClassInfo.dispatchingTimeoutMillis));
131 mInfo.frameLeft = env->GetIntField(obj,
132 gInputWindowHandleClassInfo.frameLeft);
133 mInfo.frameTop = env->GetIntField(obj,
134 gInputWindowHandleClassInfo.frameTop);
135 mInfo.frameRight = env->GetIntField(obj,
136 gInputWindowHandleClassInfo.frameRight);
137 mInfo.frameBottom = env->GetIntField(obj,
138 gInputWindowHandleClassInfo.frameBottom);
139 mInfo.surfaceInset = env->GetIntField(obj,
140 gInputWindowHandleClassInfo.surfaceInset);
141 mInfo.globalScaleFactor = env->GetFloatField(obj,
142 gInputWindowHandleClassInfo.scaleFactor);
143
144 jobject regionObj = env->GetObjectField(obj,
145 gInputWindowHandleClassInfo.touchableRegion);
146 if (regionObj) {
147 for (graphics::RegionIterator it(env, regionObj); !it.isDone(); it.next()) {
148 ARect rect = it.getRect();
149 mInfo.addTouchableRegion(Rect(rect.left, rect.top, rect.right, rect.bottom));
150 }
151 env->DeleteLocalRef(regionObj);
152 }
153
154 const auto flags = ftl::Flags<WindowInfo::Flag>(
155 env->GetIntField(obj, gInputWindowHandleClassInfo.layoutParamsFlags));
156 const auto type = static_cast<WindowInfo::Type>(
157 env->GetIntField(obj, gInputWindowHandleClassInfo.layoutParamsType));
158 mInfo.layoutParamsFlags = flags;
159 mInfo.layoutParamsType = type;
160
161 mInfo.inputConfig = static_cast<gui::WindowInfo::InputConfig>(
162 env->GetIntField(obj, gInputWindowHandleClassInfo.inputConfig));
163
164 mInfo.touchOcclusionMode = static_cast<TouchOcclusionMode>(
165 env->GetIntField(obj, gInputWindowHandleClassInfo.touchOcclusionMode));
166 mInfo.ownerPid = gui::Pid{env->GetIntField(obj, gInputWindowHandleClassInfo.ownerPid)};
167 mInfo.ownerUid = gui::Uid{
168 static_cast<uid_t>(env->GetIntField(obj, gInputWindowHandleClassInfo.ownerUid))};
169 mInfo.packageName = getStringField(env, obj, gInputWindowHandleClassInfo.packageName, "<null>");
170 mInfo.displayId = env->GetIntField(obj,
171 gInputWindowHandleClassInfo.displayId);
172
173 jobject inputApplicationHandleObj = env->GetObjectField(obj,
174 gInputWindowHandleClassInfo.inputApplicationHandle);
175 if (inputApplicationHandleObj) {
176 std::shared_ptr<InputApplicationHandle> inputApplicationHandle =
177 android_view_InputApplicationHandle_getHandle(env, inputApplicationHandleObj);
178 if (inputApplicationHandle != nullptr) {
179 inputApplicationHandle->updateInfo();
180 mInfo.applicationInfo = *(inputApplicationHandle->getInfo());
181 }
182 env->DeleteLocalRef(inputApplicationHandleObj);
183 }
184
185 mInfo.replaceTouchableRegionWithCrop = env->GetBooleanField(obj,
186 gInputWindowHandleClassInfo.replaceTouchableRegionWithCrop);
187
188 jobject weakSurfaceCtrl = env->GetObjectField(obj,
189 gInputWindowHandleClassInfo.touchableRegionSurfaceControl.ctrl);
190 bool touchableRegionCropHandleSet = false;
191 if (weakSurfaceCtrl) {
192 // Promote java weak reference.
193 jobject strongSurfaceCtrl = env->CallObjectMethod(weakSurfaceCtrl,
194 gInputWindowHandleClassInfo.touchableRegionSurfaceControl.get);
195 if (strongSurfaceCtrl) {
196 jlong mNativeObject = env->GetLongField(strongSurfaceCtrl,
197 gInputWindowHandleClassInfo.touchableRegionSurfaceControl.mNativeObject);
198 if (mNativeObject) {
199 auto ctrl = reinterpret_cast<SurfaceControl *>(mNativeObject);
200 mInfo.touchableRegionCropHandle = ctrl->getHandle();
201 touchableRegionCropHandleSet = true;
202 }
203 env->DeleteLocalRef(strongSurfaceCtrl);
204 }
205 env->DeleteLocalRef(weakSurfaceCtrl);
206 }
207 if (!touchableRegionCropHandleSet) {
208 mInfo.touchableRegionCropHandle.clear();
209 }
210
211 jobject windowTokenObj = env->GetObjectField(obj, gInputWindowHandleClassInfo.windowToken);
212 if (windowTokenObj) {
213 mInfo.windowToken = ibinderForJavaObject(env, windowTokenObj);
214 env->DeleteLocalRef(windowTokenObj);
215 } else {
216 mInfo.windowToken.clear();
217 }
218
219 ScopedLocalRef<jobject>
220 focusTransferTargetObj(env,
221 env->GetObjectField(obj,
222 gInputWindowHandleClassInfo
223 .focusTransferTarget));
224 if (focusTransferTargetObj.get()) {
225 mInfo.focusTransferTarget = ibinderForJavaObject(env, focusTransferTargetObj.get());
226 } else {
227 mInfo.focusTransferTarget.clear();
228 }
229
230 env->DeleteLocalRef(obj);
231 return true;
232 }
233
234
235 // --- Global functions ---
236
android_view_InputWindowHandle_getHandle(JNIEnv * env,jobject inputWindowHandleObj)237 sp<NativeInputWindowHandle> android_view_InputWindowHandle_getHandle(
238 JNIEnv* env, jobject inputWindowHandleObj) {
239 if (!inputWindowHandleObj) {
240 return NULL;
241 }
242
243 AutoMutex _l(gHandleMutex);
244
245 jlong ptr = env->GetLongField(inputWindowHandleObj, gInputWindowHandleClassInfo.ptr);
246 NativeInputWindowHandle* handle;
247 if (ptr) {
248 handle = reinterpret_cast<NativeInputWindowHandle*>(ptr);
249 } else {
250 jweak objWeak = env->NewWeakGlobalRef(inputWindowHandleObj);
251 handle = new NativeInputWindowHandle(objWeak);
252 handle->incStrong((void*)android_view_InputWindowHandle_getHandle);
253 env->SetLongField(inputWindowHandleObj, gInputWindowHandleClassInfo.ptr,
254 reinterpret_cast<jlong>(handle));
255 }
256 return handle;
257 }
258
android_view_InputWindowHandle_fromWindowInfo(JNIEnv * env,gui::WindowInfo windowInfo)259 jobject android_view_InputWindowHandle_fromWindowInfo(JNIEnv* env, gui::WindowInfo windowInfo) {
260 ScopedLocalRef<jobject>
261 applicationHandle(env,
262 android_view_InputApplicationHandle_fromInputApplicationInfo(
263 env, windowInfo.applicationInfo));
264
265 jobject inputWindowHandle =
266 env->NewObject(gInputWindowHandleClassInfo.clazz, gInputWindowHandleClassInfo.ctor,
267 applicationHandle.get(), windowInfo.displayId);
268 if (env->ExceptionCheck()) {
269 LOGE_EX(env);
270 env->ExceptionClear();
271 }
272 LOG_ALWAYS_FATAL_IF(inputWindowHandle == nullptr,
273 "Failed to create new InputWindowHandle object.");
274 env->SetObjectField(inputWindowHandle, gInputWindowHandleClassInfo.token,
275 javaObjectForIBinder(env, windowInfo.token));
276 ScopedLocalRef<jstring> name(env, env->NewStringUTF(windowInfo.name.data()));
277 env->SetObjectField(inputWindowHandle, gInputWindowHandleClassInfo.name, name.get());
278 env->SetIntField(inputWindowHandle, gInputWindowHandleClassInfo.layoutParamsFlags,
279 static_cast<uint32_t>(windowInfo.layoutParamsFlags.get()));
280 env->SetIntField(inputWindowHandle, gInputWindowHandleClassInfo.layoutParamsType,
281 static_cast<int32_t>(windowInfo.layoutParamsType));
282 env->SetLongField(inputWindowHandle, gInputWindowHandleClassInfo.dispatchingTimeoutMillis,
283 std::chrono::duration_cast<std::chrono::milliseconds>(
284 windowInfo.dispatchingTimeout)
285 .count());
286 env->SetIntField(inputWindowHandle, gInputWindowHandleClassInfo.frameLeft,
287 windowInfo.frameLeft);
288 env->SetIntField(inputWindowHandle, gInputWindowHandleClassInfo.frameTop, windowInfo.frameTop);
289 env->SetIntField(inputWindowHandle, gInputWindowHandleClassInfo.frameRight,
290 windowInfo.frameRight);
291 env->SetIntField(inputWindowHandle, gInputWindowHandleClassInfo.frameBottom,
292 windowInfo.frameBottom);
293 env->SetIntField(inputWindowHandle, gInputWindowHandleClassInfo.surfaceInset,
294 windowInfo.surfaceInset);
295 env->SetFloatField(inputWindowHandle, gInputWindowHandleClassInfo.scaleFactor,
296 windowInfo.globalScaleFactor);
297
298 SkRegion* region = new SkRegion();
299 for (const auto& r : windowInfo.touchableRegion) {
300 region->op({r.left, r.top, r.right, r.bottom}, SkRegion::kUnion_Op);
301 }
302 ScopedLocalRef<jobject> regionObj(env,
303 env->NewObject(gRegionClassInfo.clazz, gRegionClassInfo.ctor,
304 reinterpret_cast<jlong>(region)));
305 env->SetObjectField(inputWindowHandle, gInputWindowHandleClassInfo.touchableRegion,
306 regionObj.get());
307
308 env->SetIntField(inputWindowHandle, gInputWindowHandleClassInfo.touchOcclusionMode,
309 static_cast<int32_t>(windowInfo.touchOcclusionMode));
310 env->SetIntField(inputWindowHandle, gInputWindowHandleClassInfo.ownerPid,
311 windowInfo.ownerPid.val());
312 env->SetIntField(inputWindowHandle, gInputWindowHandleClassInfo.ownerUid,
313 windowInfo.ownerUid.val());
314 ScopedLocalRef<jstring> packageName(env, env->NewStringUTF(windowInfo.packageName.data()));
315 env->SetObjectField(inputWindowHandle, gInputWindowHandleClassInfo.packageName,
316 packageName.get());
317
318 const auto inputConfig = windowInfo.inputConfig.get();
319 static_assert(sizeof(inputConfig) == sizeof(int32_t));
320 env->SetIntField(inputWindowHandle, gInputWindowHandleClassInfo.inputConfig,
321 static_cast<int32_t>(inputConfig));
322
323 float transformVals[9];
324 for (int i = 0; i < 9; i++) {
325 transformVals[i] = windowInfo.transform[i % 3][i / 3];
326 }
327 ScopedLocalRef<jobject> matrixObj(env, AMatrix_newInstance(env, transformVals));
328 env->SetObjectField(inputWindowHandle, gInputWindowHandleClassInfo.transform, matrixObj.get());
329
330 env->SetObjectField(inputWindowHandle, gInputWindowHandleClassInfo.windowToken,
331 javaObjectForIBinder(env, windowInfo.windowToken));
332
333 return inputWindowHandle;
334 }
335
336 // --- JNI ---
337
android_view_InputWindowHandle_nativeDispose(JNIEnv * env,jobject obj)338 static void android_view_InputWindowHandle_nativeDispose(JNIEnv* env, jobject obj) {
339 AutoMutex _l(gHandleMutex);
340
341 jlong ptr = env->GetLongField(obj, gInputWindowHandleClassInfo.ptr);
342 if (ptr) {
343 env->SetLongField(obj, gInputWindowHandleClassInfo.ptr, 0);
344
345 NativeInputWindowHandle* handle = reinterpret_cast<NativeInputWindowHandle*>(ptr);
346 handle->decStrong((void*)android_view_InputWindowHandle_getHandle);
347 }
348 }
349
350
351 static const JNINativeMethod gInputWindowHandleMethods[] = {
352 /* name, signature, funcPtr */
353 { "nativeDispose", "()V",
354 (void*) android_view_InputWindowHandle_nativeDispose },
355 };
356
357 #define FIND_CLASS(var, className) \
358 var = env->FindClass(className); \
359 LOG_FATAL_IF(! (var), "Unable to find class " className);
360
361 #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
362 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
363 LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
364
365 #define GET_METHOD_ID(var, clazz, methodName, methodSignature) \
366 var = env->GetMethodID(clazz, methodName, methodSignature); \
367 LOG_FATAL_IF(! (var), "Unable to find method " methodName);
368
register_android_view_InputWindowHandle(JNIEnv * env)369 int register_android_view_InputWindowHandle(JNIEnv* env) {
370 int res = jniRegisterNativeMethods(env, "android/view/InputWindowHandle",
371 gInputWindowHandleMethods, NELEM(gInputWindowHandleMethods));
372 (void) res; // Faked use when LOG_NDEBUG.
373 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
374
375 jclass clazz;
376 FIND_CLASS(clazz, "android/view/InputWindowHandle");
377 gInputWindowHandleClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
378
379 GET_METHOD_ID(gInputWindowHandleClassInfo.ctor, clazz, "<init>",
380 "(Landroid/view/InputApplicationHandle;I)V");
381
382 GET_FIELD_ID(gInputWindowHandleClassInfo.ptr, clazz,
383 "ptr", "J");
384
385 GET_FIELD_ID(gInputWindowHandleClassInfo.inputApplicationHandle, clazz,
386 "inputApplicationHandle", "Landroid/view/InputApplicationHandle;");
387
388 GET_FIELD_ID(gInputWindowHandleClassInfo.token, clazz,
389 "token", "Landroid/os/IBinder;");
390
391 GET_FIELD_ID(gInputWindowHandleClassInfo.name, clazz,
392 "name", "Ljava/lang/String;");
393
394 GET_FIELD_ID(gInputWindowHandleClassInfo.layoutParamsFlags, clazz,
395 "layoutParamsFlags", "I");
396
397 GET_FIELD_ID(gInputWindowHandleClassInfo.layoutParamsType, clazz,
398 "layoutParamsType", "I");
399
400 GET_FIELD_ID(gInputWindowHandleClassInfo.dispatchingTimeoutMillis, clazz,
401 "dispatchingTimeoutMillis", "J");
402
403 GET_FIELD_ID(gInputWindowHandleClassInfo.frameLeft, clazz,
404 "frameLeft", "I");
405
406 GET_FIELD_ID(gInputWindowHandleClassInfo.frameTop, clazz,
407 "frameTop", "I");
408
409 GET_FIELD_ID(gInputWindowHandleClassInfo.frameRight, clazz,
410 "frameRight", "I");
411
412 GET_FIELD_ID(gInputWindowHandleClassInfo.frameBottom, clazz,
413 "frameBottom", "I");
414
415 GET_FIELD_ID(gInputWindowHandleClassInfo.surfaceInset, clazz,
416 "surfaceInset", "I");
417
418 GET_FIELD_ID(gInputWindowHandleClassInfo.scaleFactor, clazz,
419 "scaleFactor", "F");
420
421 GET_FIELD_ID(gInputWindowHandleClassInfo.touchableRegion, clazz,
422 "touchableRegion", "Landroid/graphics/Region;");
423
424 GET_FIELD_ID(gInputWindowHandleClassInfo.touchOcclusionMode, clazz, "touchOcclusionMode", "I");
425
426 GET_FIELD_ID(gInputWindowHandleClassInfo.ownerPid, clazz,
427 "ownerPid", "I");
428
429 GET_FIELD_ID(gInputWindowHandleClassInfo.ownerUid, clazz,
430 "ownerUid", "I");
431
432 GET_FIELD_ID(gInputWindowHandleClassInfo.packageName, clazz, "packageName",
433 "Ljava/lang/String;");
434
435 GET_FIELD_ID(gInputWindowHandleClassInfo.inputConfig, clazz, "inputConfig", "I");
436
437 GET_FIELD_ID(gInputWindowHandleClassInfo.displayId, clazz,
438 "displayId", "I");
439
440 GET_FIELD_ID(gInputWindowHandleClassInfo.replaceTouchableRegionWithCrop, clazz,
441 "replaceTouchableRegionWithCrop", "Z");
442
443 GET_FIELD_ID(gInputWindowHandleClassInfo.transform, clazz, "transform",
444 "Landroid/graphics/Matrix;");
445
446 GET_FIELD_ID(gInputWindowHandleClassInfo.windowToken, clazz, "windowToken",
447 "Landroid/os/IBinder;");
448
449 GET_FIELD_ID(gInputWindowHandleClassInfo.focusTransferTarget, clazz, "focusTransferTarget",
450 "Landroid/os/IBinder;");
451
452 jclass weakRefClazz;
453 FIND_CLASS(weakRefClazz, "java/lang/ref/Reference");
454
455 GET_METHOD_ID(gInputWindowHandleClassInfo.touchableRegionSurfaceControl.get, weakRefClazz,
456 "get", "()Ljava/lang/Object;")
457
458 GET_FIELD_ID(gInputWindowHandleClassInfo.touchableRegionSurfaceControl.ctrl, clazz,
459 "touchableRegionSurfaceControl", "Ljava/lang/ref/WeakReference;");
460
461 jclass surfaceControlClazz;
462 FIND_CLASS(surfaceControlClazz, "android/view/SurfaceControl");
463 GET_FIELD_ID(gInputWindowHandleClassInfo.touchableRegionSurfaceControl.mNativeObject,
464 surfaceControlClazz, "mNativeObject", "J");
465
466 jclass regionClazz;
467 FIND_CLASS(regionClazz, "android/graphics/Region");
468 gRegionClassInfo.clazz = MakeGlobalRefOrDie(env, regionClazz);
469 GET_METHOD_ID(gRegionClassInfo.ctor, gRegionClassInfo.clazz, "<init>", "(J)V");
470 return 0;
471 }
472
473 } /* namespace android */
474