From bb005f787f4e00bd832e6a78797be10af2994061 Mon Sep 17 00:00:00 2001 From: Ken Wakasa Date: Wed, 8 Aug 2012 20:43:47 +0900 Subject: [PATCH] Start using JNI's GetArrayRegion() Change-Id: Iaa1014efe639dd79540835b0d5201093dbd8dab8 --- ...oid_inputmethod_keyboard_ProximityInfo.cpp | 42 ++++---------- native/jni/jni_common.h | 27 --------- native/jni/src/proximity_info.cpp | 56 ++++++++++--------- native/jni/src/proximity_info.h | 13 +++-- 4 files changed, 48 insertions(+), 90 deletions(-) diff --git a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp index 0f2c2da43..a01ac3780 100644 --- a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp +++ b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp @@ -25,43 +25,21 @@ namespace latinime { static jlong latinime_Keyboard_setProximityInfo(JNIEnv *env, jobject object, jstring localeJStr, jint maxProximityCharsSize, jint displayWidth, jint displayHeight, - jint gridWidth, jint gridHeight, jint mostCommonkeyWidth, jintArray proximityCharsArray, - jint keyCount, jintArray keyXCoordinateArray, jintArray keyYCoordinateArray, - jintArray keyWidthArray, jintArray keyHeightArray, jintArray keyCharCodeArray, - jfloatArray sweetSpotCenterXArray, jfloatArray sweetSpotCenterYArray, - jfloatArray sweetSpotRadiusArray) { + jint gridWidth, jint gridHeight, jint mostCommonkeyWidth, jintArray proximityChars, + jint keyCount, jintArray keyXCoordinates, jintArray keyYCoordinates, + jintArray keyWidths, jintArray keyHeights, jintArray keyCharCodes, + jfloatArray sweetSpotCenterXs, jfloatArray sweetSpotCenterYs, jfloatArray sweetSpotRadii) { const char *localeCStr = env->GetStringUTFChars(localeJStr, 0); - jint *proximityChars = env->GetIntArrayElements(proximityCharsArray, 0); - jint *keyXCoordinates = safeGetIntArrayElements(env, keyXCoordinateArray); - jint *keyYCoordinates = safeGetIntArrayElements(env, keyYCoordinateArray); - jint *keyWidths = safeGetIntArrayElements(env, keyWidthArray); - jint *keyHeights = safeGetIntArrayElements(env, keyHeightArray); - jint *keyCharCodes = safeGetIntArrayElements(env, keyCharCodeArray); - jfloat *sweetSpotCenterXs = safeGetFloatArrayElements(env, sweetSpotCenterXArray); - jfloat *sweetSpotCenterYs = safeGetFloatArrayElements(env, sweetSpotCenterYArray); - jfloat *sweetSpotRadii = safeGetFloatArrayElements(env, sweetSpotRadiusArray); - ProximityInfo *proximityInfo = new ProximityInfo( - localeCStr, maxProximityCharsSize, displayWidth, displayHeight, gridWidth, gridHeight, - mostCommonkeyWidth, (const int32_t*)proximityChars, keyCount, - (const int32_t*)keyXCoordinates, (const int32_t*)keyYCoordinates, - (const int32_t*)keyWidths, (const int32_t*)keyHeights, (const int32_t*)keyCharCodes, - (const float*)sweetSpotCenterXs, (const float*)sweetSpotCenterYs, - (const float*)sweetSpotRadii); - safeReleaseFloatArrayElements(env, sweetSpotRadiusArray, sweetSpotRadii); - safeReleaseFloatArrayElements(env, sweetSpotCenterYArray, sweetSpotCenterYs); - safeReleaseFloatArrayElements(env, sweetSpotCenterXArray, sweetSpotCenterXs); - safeReleaseIntArrayElements(env, keyCharCodeArray, keyCharCodes); - safeReleaseIntArrayElements(env, keyHeightArray, keyHeights); - safeReleaseIntArrayElements(env, keyWidthArray, keyWidths); - safeReleaseIntArrayElements(env, keyYCoordinateArray, keyYCoordinates); - safeReleaseIntArrayElements(env, keyXCoordinateArray, keyXCoordinates); - env->ReleaseIntArrayElements(proximityCharsArray, proximityChars, 0); + ProximityInfo *proximityInfo = new ProximityInfo(env, localeCStr, maxProximityCharsSize, + displayWidth, displayHeight, gridWidth, gridHeight, mostCommonkeyWidth, proximityChars, + keyCount, keyXCoordinates, keyYCoordinates, keyWidths, keyHeights, keyCharCodes, + sweetSpotCenterXs, sweetSpotCenterYs, sweetSpotRadii); env->ReleaseStringUTFChars(localeJStr, localeCStr); - return (jlong)proximityInfo; + return reinterpret_cast(proximityInfo); } static void latinime_Keyboard_release(JNIEnv *env, jobject object, jlong proximityInfo) { - ProximityInfo *pi = (ProximityInfo*)proximityInfo; + ProximityInfo *pi = reinterpret_cast(proximityInfo); if (!pi) return; delete pi; } diff --git a/native/jni/jni_common.h b/native/jni/jni_common.h index 658ff18b9..993f97e80 100644 --- a/native/jni/jni_common.h +++ b/native/jni/jni_common.h @@ -24,32 +24,5 @@ namespace latinime { int registerNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *methods, int numMethods); -inline jint *safeGetIntArrayElements(JNIEnv *env, jintArray jArray) { - if (jArray) { - return env->GetIntArrayElements(jArray, 0); - } else { - return 0; - } -} - -inline jfloat *safeGetFloatArrayElements(JNIEnv *env, jfloatArray jArray) { - if (jArray) { - return env->GetFloatArrayElements(jArray, 0); - } else { - return 0; - } -} - -inline void safeReleaseIntArrayElements(JNIEnv *env, jintArray jArray, jint *cArray) { - if (jArray) { - env->ReleaseIntArrayElements(jArray, cArray, 0); - } -} - -inline void safeReleaseFloatArrayElements(JNIEnv *env, jfloatArray jArray, jfloat *cArray) { - if (jArray) { - env->ReleaseFloatArrayElements(jArray, cArray, 0); - } -} } // namespace latinime #endif // LATINIME_JNI_COMMON_H diff --git a/native/jni/src/proximity_info.cpp b/native/jni/src/proximity_info.cpp index e9cb14e63..7bae41362 100644 --- a/native/jni/src/proximity_info.cpp +++ b/native/jni/src/proximity_info.cpp @@ -24,25 +24,36 @@ #include "additional_proximity_chars.h" #include "char_utils.h" #include "defines.h" +#include "jni.h" #include "proximity_info.h" namespace latinime { -inline void copyOrFillZero(void *to, const void *from, size_t size) { - if (from) { - memcpy(to, from, size); - } else { - memset(to, 0, size); +static inline void safeGetOrFillZeroIntArrayRegion(JNIEnv *env, jintArray jArray, jsize len, + jint *buffer) { + if (jArray && buffer) { + env->GetIntArrayRegion(jArray, 0, len, buffer); + } else if (buffer) { + memset(buffer, 0, len); } } -ProximityInfo::ProximityInfo(const char *localeCStr, const int maxProximityCharsSize, +static inline void safeGetOrFillZeroFloatArrayRegion(JNIEnv *env, jfloatArray jArray, jsize len, + jfloat *buffer) { + if (jArray && buffer) { + env->GetFloatArrayRegion(jArray, 0, len, buffer); + } else if (buffer) { + memset(buffer, 0, len); + } +} + +ProximityInfo::ProximityInfo(JNIEnv *env, const char *localeCStr, const int maxProximityCharsSize, const int keyboardWidth, const int keyboardHeight, const int gridWidth, - const int gridHeight, const int mostCommonKeyWidth, - const int32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates, - const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights, - const int32_t *keyCharCodes, const float *sweetSpotCenterXs, const float *sweetSpotCenterYs, - const float *sweetSpotRadii) + const int gridHeight, const int mostCommonKeyWidth, const jintArray proximityChars, + const int keyCount, const jintArray keyXCoordinates, const jintArray keyYCoordinates, + const jintArray keyWidths, const jintArray keyHeights, const jintArray keyCharCodes, + const jfloatArray sweetSpotCenterXs, const jfloatArray sweetSpotCenterYs, + const jfloatArray sweetSpotRadii) : MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), KEYBOARD_WIDTH(keyboardWidth), KEYBOARD_HEIGHT(keyboardHeight), GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight), MOST_COMMON_KEY_WIDTH_SQUARE(mostCommonKeyWidth * mostCommonKeyWidth), @@ -58,20 +69,15 @@ ProximityInfo::ProximityInfo(const char *localeCStr, const int maxProximityChars AKLOGI("Create proximity info array %d", proximityGridLength); } mProximityCharsArray = new int32_t[proximityGridLength]; - memcpy(mProximityCharsArray, proximityCharsArray, - proximityGridLength * sizeof(mProximityCharsArray[0])); - - copyOrFillZero(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0])); - copyOrFillZero(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0])); - copyOrFillZero(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0])); - copyOrFillZero(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0])); - copyOrFillZero(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0])); - copyOrFillZero(mSweetSpotCenterXs, sweetSpotCenterXs, - KEY_COUNT * sizeof(mSweetSpotCenterXs[0])); - copyOrFillZero(mSweetSpotCenterYs, sweetSpotCenterYs, - KEY_COUNT * sizeof(mSweetSpotCenterYs[0])); - copyOrFillZero(mSweetSpotRadii, sweetSpotRadii, KEY_COUNT * sizeof(mSweetSpotRadii[0])); - + safeGetOrFillZeroIntArrayRegion(env, proximityChars, proximityGridLength, mProximityCharsArray); + safeGetOrFillZeroIntArrayRegion(env, keyXCoordinates, KEY_COUNT, mKeyXCoordinates); + safeGetOrFillZeroIntArrayRegion(env, keyYCoordinates, KEY_COUNT, mKeyYCoordinates); + safeGetOrFillZeroIntArrayRegion(env, keyWidths, KEY_COUNT, mKeyWidths); + safeGetOrFillZeroIntArrayRegion(env, keyHeights, KEY_COUNT, mKeyHeights); + safeGetOrFillZeroIntArrayRegion(env, keyCharCodes, KEY_COUNT, mKeyCharCodes); + safeGetOrFillZeroFloatArrayRegion(env, sweetSpotCenterXs, KEY_COUNT, mSweetSpotCenterXs); + safeGetOrFillZeroFloatArrayRegion(env, sweetSpotCenterYs, KEY_COUNT, mSweetSpotCenterYs); + safeGetOrFillZeroFloatArrayRegion(env, sweetSpotRadii, KEY_COUNT, mSweetSpotRadii); initializeCodeToKeyIndex(); } diff --git a/native/jni/src/proximity_info.h b/native/jni/src/proximity_info.h index 6c5c6bf99..5dd378d21 100644 --- a/native/jni/src/proximity_info.h +++ b/native/jni/src/proximity_info.h @@ -21,6 +21,7 @@ #include #include "defines.h" +#include "jni.h" namespace latinime { @@ -28,13 +29,13 @@ class Correction; class ProximityInfo { public: - ProximityInfo(const char *localeCStr, const int maxProximityCharsSize, + ProximityInfo(JNIEnv *env, const char *localeCStr, const int maxProximityCharsSize, const int keyboardWidth, const int keyboardHeight, const int gridWidth, - const int gridHeight, const int mostCommonkeyWidth, - const int32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates, - const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights, - const int32_t *keyCharCodes, const float *sweetSpotCenterXs, - const float *sweetSpotCenterYs, const float *sweetSpotRadii); + const int gridHeight, const int mostCommonKeyWidth, const jintArray proximityChars, + const int keyCount, const jintArray keyXCoordinates, const jintArray keyYCoordinates, + const jintArray keyWidths, const jintArray keyHeights, const jintArray keyCharCodes, + const jfloatArray sweetSpotCenterXs, const jfloatArray sweetSpotCenterYs, + const jfloatArray sweetSpotRadii); ~ProximityInfo(); bool hasSpaceProximity(const int x, const int y) const; int getNormalizedSquaredDistance(const int inputIndex, const int proximityIndex) const;